<?php

namespace App\Models\System;

use App\Models\Model;
use App\Models\ActivityInfo;
use App\Models\BasicInfoPosition;
use App\Models\Rating\JobApplyDetails;
use App\Models\Rating\JobApplyScores;
use App\Models\Educaton\MasterTeacher;

class ScaleDetail extends Model
{
    protected $table = 'scale_details';
    protected $guarded = [];
    protected $hidden = ['created_at','updated_at'];
    protected $appends = [
       'module_text',
       'apply_data'
    ];

    public function child()
    {
        return $this->hasMany(self::class, 'pid')->with('child');
    }
    
    public function parent()
    {
        return $this->belongsTo(self::class, 'pid')
            ->with('parent')
            ->withCount('child');
    }

    public function scale()
    {
        return $this->belongsTo(Scale::class, 'scale_id');
    }

    public function jobApplyDetails()
    {
        return $this->hasOne(JobApplyDetails::class, 'scale_id');
    }

    public function jobApplyScores()
    {
        return $this->hasOne(JobApplyScores::class, 'scale_id');
    }

    public function getApplyDataAttribute(){
        $jobApplyDetails = !empty($this->relations['jobApplyDetails']) ? $this->relations['jobApplyDetails'] : '';
        if($jobApplyDetails){
            $scale_index = $jobApplyDetails->scale_index;
            $modules = json_decode($jobApplyDetails->modules,true) ;
            $modules_id = $jobApplyDetails->modules_id;
            
            switch($scale_index){
                case 1:
                  $result =  ActivityInfo::select('b.data_version_name','activity_info.id','activity_info.name','activity_info.begin_date','activity_info.end_date','activity_info.level','activity_info.grade','activity_info.category','activity_info.proof_material','b.status','c.participant','d.nickname')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->leftJoin('event_statistics as c','c.activity_info_id','=','activity_info.id')
                    ->leftJoin('users as d','activity_info.creator_id','=','d.id')
                    ->whereIn('activity_info.id',$modules)           
                    ->orderBy('activity_info.created_at','desc')
                    ->get()
                    ->toArray();               
                    break;
                case 2:
                    $result = ActivityInfo::select('activity_info.*', 'b.status')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->with('term', 'forminstance', 'compere', 'participant', 'creator', 'instance:id,status')
                    ->whereIn('activity_info.id',$modules)  
                    ->orderBy('activity_info.id', 'desc')
                    ->get()
                    ->toArray();
                    break;
                case 3:
                    $result = ActivityInfo::select('activity_info.*', 'b.status')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->with('term', 'forminstance', 'compere', 'participant', 'creator', 'instance:id,status')
                    ->whereIn('activity_info.id',$modules)  
                    ->orderBy('activity_info.id', 'desc')
                    ->get()
                    ->toArray();
                    break;
                case 4:
                    if($modules_id == 23){
                        $result = BasicInfoPosition::whereIn('id',$modules)->with('term')->get()->toArray();
                    }else{
                        $result = MasterTeacher::whereDeleted(0)
                        ->with('term', 'instance:id,status')
                        ->whereIn('id',$modules) 
                        ->orderBy('id', 'desc')
                        ->get()
                        ->toArray();
                    }                   
                    break; 
                case 5:
                    $result = ActivityInfo::select('activity_info.*', 'b.status')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->with('term', 'forminstance', 'compere', 'participant', 'creator', 'instance:id,status')
                    ->whereIn('activity_info.id',$modules)  
                    ->orderBy('activity_info.id', 'desc')
                    ->get()
                    ->toArray();
                    break; 
                case 6:
                    $result = ActivityInfo::select('b.data_version_name','activity_info.id','activity_info.name','activity_info.begin_date','activity_info.end_date','activity_info.level','activity_info.grade','activity_info.category','activity_info.proof_material','b.status','c.participant','d.nickname')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->leftJoin('event_statistics as c','c.activity_info_id','=','activity_info.id')
                    ->leftJoin('users as d','activity_info.creator_id','=','d.id')
                    ->whereIn('activity_info.id',$modules) 
                    ->orderBy('activity_info.created_at','desc')
                    ->get()
                    ->toArray();
                    break;
                case 7:
                    $result = ActivityInfo::select('b.data_version_name','activity_info.id','activity_info.name','activity_info.begin_date','activity_info.end_date','activity_info.level','activity_info.grade','activity_info.category','activity_info.proof_material','b.status','c.participant','d.nickname','activity_info.teacher_name','activity_info.school_name')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->leftJoin('event_statistics as c','c.activity_info_id','=','activity_info.id')
                    ->leftJoin('users as d','activity_info.creator_id','=','d.id')
                    ->whereIn('activity_info.id',$modules) 
                    ->orderBy('activity_info.created_at','desc')
                    ->get()
                    ->toArray();
                    break;
                case 8:   
                    $result = ActivityInfo::select('b.data_version_name','activity_info.id','activity_info.name','activity_info.begin_date','activity_info.end_date','activity_info.level','activity_info.grade','activity_info.category','activity_info.proof_material','b.status','c.participant','d.nickname','activity_info.teacher_name','activity_info.school_name')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->leftJoin('event_statistics as c','c.activity_info_id','=','activity_info.id')
                    ->leftJoin('users as d','activity_info.creator_id','=','d.id')
                    ->with('compere')
                    ->whereIn('activity_info.id',$modules) 
                    ->orderBy('activity_info.created_at','desc')
                    ->get()
                    ->toArray();
                    break; 
                case 9:
                    $data = ActivityInfo::select('activity_info.date','activity_info.activity_category','b.data_version_name','activity_info.id','activity_info.name','activity_info.begin_date','activity_info.end_date','activity_info.level','activity_info.grade','activity_info.category','activity_info.proof_material','b.status','c.participant','d.nickname','activity_info.teacher_name','activity_info.school_name')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->leftJoin('event_statistics as c','c.activity_info_id','=','activity_info.id')
                    ->leftJoin('users as d','activity_info.creator_id','=','d.id')
                    ->whereIn('activity_info.id',$modules) 
                    ->with('participants')   
                    ->orderBy('activity_info.created_at','desc')
                    ->get()
                    ->toArray();
                    //dd($data);
                    $result = [];
                    foreach($data as $k=>$v){
                        $result[$v['activity_category']][] = $v;
                    }
                
                    break;
                case 10:
                    $result = ActivityInfo::select('activity_info.*', 'b.status')
                    ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')
                    ->with('term', 'forminstance', 'compere', 'participant', 'creator', 'instance:id,status')
                    ->whereIn('activity_info.id',$modules)  
                    ->orderBy('activity_info.id', 'desc')
                    ->get()
                    ->toArray();
                    break;
                default: 
            }
            return $result;
        }
        return [];
    }

    public function getModuleTextAttribute()
    {
        static  $dic = null;
        if(!$dic){
            $dic = Dicitem::get()->toArray();
        }
        
        if(!$dic){
            return '';
        }
        //$dic = array_column($dic,null,'diccode');
        $dics = [];
        foreach($dic as $k=>$v){
            $dics[$v['diccode']][$v['itemcode']] = $v;
        }
      
        return !empty($dics['DYMK'][$this->attributes['module']])?$dics['DYMK'][$this->attributes['module']]['itemname']:'';
        
    }

}
